package com.lianbang.staff.module.user

import android.os.Bundle
import android.os.CountDownTimer
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.View
import cn.ubest.mvvmcore.base.BaseFragment
import cn.ubest.mvvmcore.utils.DoubleClickHelper
import cn.ubest.mvvmcore.utils.ResourcesUtils
import cn.ubest.mvvmcore.utils.SpanUtils
import cn.ubest.mvvmcore.utils.ToastUtils
import com.lianbang.staff.R
import com.lianbang.staff.databinding.FragmentVerifyCodeBinding
import com.lianbang.staff.vm.UserVM
import kotlinx.android.synthetic.main.fragment_verify_code.*

/**
 * Created by maoqi on 2021/1/12.
 */
class VerifyCodeFragment : BaseFragment<UserVM, FragmentVerifyCodeBinding>() {

    companion object {
        const val COUNT_DOWN_TIME = 60 * 1000
        fun newInstance(): VerifyCodeFragment {
            val args = Bundle()
            val fragment = VerifyCodeFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initLayout(): Int {
        return R.layout.fragment_verify_code
    }

    override fun initData(arguments: Bundle?) {

    }

    private var isSend = false
    override fun initView(view: View) {
        ll_agreement.visibility = if (activity is FindPasswordActivity) {
            View.GONE
        } else {
            View.VISIBLE
        }
        tv_agreement.text = SpanUtils
            .with(tv_agreement)
            .append("请同意")
            .append("《用户协议》")
            .setForegroundColor(ResourcesUtils.getColor(R.color.colorPrimary))
            .setClickSpan(object : ClickableSpan() {
                override fun onClick(widget: View) {
                    ToastUtils.toast("用户协议")
                }

                override fun updateDrawState(ds: TextPaint) {
                    super.updateDrawState(ds)
                    ds.isUnderlineText = false
                }
            })
            .append("和")
            .append("《隐私保护协议》")
            .setForegroundColor(ResourcesUtils.getColor(R.color.colorPrimary))
            .setClickSpan(object : ClickableSpan() {
                override fun onClick(widget: View) {
                    ToastUtils.toast("隐私保护协议")
                }

                override fun updateDrawState(ds: TextPaint) {
                    super.updateDrawState(ds)
                    ds.isUnderlineText = false
                }
            })
            .create()

        btn_send_code.setOnClickListener(object : DoubleClickHelper.OnClickListener() {
            override fun onClicked(view: View?) {
                val phone = et_phone.text.toString()

                if (phone.isEmpty()) {
                    ToastUtils.toast("手机号不能为空")
                    return
                }

                if (phone.length != 11) {
                    ToastUtils.toast("手机号必须是11位")
                    return
                }

                mViewModel.sendCode(phone, "${phone}#!lianbang*$", "safp")
                    .observe(this@VerifyCodeFragment, getObserver {
                        isSend = true
                        ToastUtils.toast("发送成功")
                        startCountDownTimer(COUNT_DOWN_TIME)
                    })
            }
        })
        btn_submit.setOnClickListener(object : DoubleClickHelper.OnClickListener() {
            override fun onClicked(view: View?) {
                if (isSend) {
                    val phone = et_phone.text.toString()
                    val code = et_code.text.toString()
                    if (phone.isEmpty()) {
                        ToastUtils.toast("手机号不能为空")
                        return
                    }

                    if (phone.length != 11) {
                        ToastUtils.toast("手机号必须是11位")
                        return
                    }

                    if (code.isEmpty()) {
                        ToastUtils.toast("验证码不能为空")
                        return
                    }

                    mViewModel.checkFindPasswordCode(phone, code)
                        .observe(this@VerifyCodeFragment, getObserver {
                            activity
                                ?.supportFragmentManager
                                ?.beginTransaction()
                                ?.replace(
                                    R.id.fl_container,
                                    SetPasswordFragment.newInstance(phone, it.auth_token)
                                )
                                ?.commit()
                        })

                } else {
                    ToastUtils.toast("请先获取验证码")
                }
            }

        })
    }

    private var mTimer: CountDownTimer? = null

    fun startCountDownTimer(countDownTime: Int) {
        mTimer = object : CountDownTimer(countDownTime.toLong(), 1000) {
            override fun onTick(millisUntilFinished: Long) {
                btn_send_code.text = "${millisUntilFinished / 1000}秒"
                setGetCodeButtonEnable(false)
            }

            override fun onFinish() {
                btn_send_code.text = "获取验证码"
                setGetCodeButtonEnable(true)
            }
        }
        setGetCodeButtonEnable(false)
        mTimer?.start()
    }

    private fun setGetCodeButtonEnable(enable: Boolean) {
        if (enable) {
            btn_send_code.isEnabled = true
            btn_send_code.setTextColor(ResourcesUtils.getColor(R.color.C_5671FB))
            btn_send_code.delegate.strokeColor = ResourcesUtils.getColor(R.color.C_5671FB)
        } else {
            btn_send_code.isEnabled = false
            btn_send_code.setTextColor(ResourcesUtils.getColor(R.color.C_D6D9DB))
            btn_send_code.delegate.strokeColor = ResourcesUtils.getColor(R.color.C_D6D9DB)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mTimer?.cancel()
    }

    override fun loadData() {

    }

}